﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUS;
using DAO;

public partial class UControl_uc_left_new : System.Web.UI.UserControl
{
    BUS_WRCATEGORY _buswrcategory = new BUS_WRCATEGORY();
    BUS_WRCATEGORYLV1 _buswrcategorylv1 = new BUS_WRCATEGORYLV1();
    public BUS_WRAREA _busArea = new BUS_WRAREA();
    public BUS_WRREGION _busRegion = new BUS_WRREGION();
    public BUS_WRCITY _busCity = new BUS_WRCITY();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string dieukien = "";
            int lang = 1;
            if (Session["SelLang"].ToString() == "vi")
            {
                lang = 2;
            }
            else
            {
                lang = 1;
            }
            if (Session["catename"] != null)
            {
                dieukien = ", p => p.Keyword == " + Session["catename"].ToString();
                foreach (WRCATEGORY _wrcategory in _buswrcategory._GetList(p => p.IDLang == lang && p.Keywords == Session["catename"].ToString()))
                {
                    string CategoryName = _wrcategory.CategoryName + " (" + _buswrcategorylv1._GetList(p => p.IDCategory == _wrcategory.ID).ToList().Count + ")";
                    TreeNode _tnCategory = new TreeNode(CategoryName, _wrcategory.ID.ToString());

                    foreach (WRCATEGORYLV1 _wrcategorylv1 in _buswrcategorylv1._GetList(p => p.IDCategory == _wrcategory.ID))
                    {
                        TreeNode _tnCategorylv1 = new TreeNode(_wrcategorylv1.CategoryLv1Name, _wrcategorylv1.ID.ToString());
                        _tnCategory.ChildNodes.Add(_tnCategorylv1);
                    }

                    trvCategory.Nodes.Add(_tnCategory);
                    trvCategory.ExpandAll();
                }
            }
            else
            {
                foreach (WRCATEGORY _wrcategory in _buswrcategory._GetList(p => p.IDLang == lang))
                {
                    string CategoryName = _wrcategory.CategoryName + " (" + _buswrcategorylv1._GetList(p => p.IDCategory == _wrcategory.ID).ToList().Count + ")";
                    TreeNode _tnCategory = new TreeNode(CategoryName, _wrcategory.ID.ToString());

                    foreach (WRCATEGORYLV1 _wrcategorylv1 in _buswrcategorylv1._GetList(p => p.IDCategory == _wrcategory.ID))
                    {
                        TreeNode _tnCategorylv1 = new TreeNode(_wrcategorylv1.CategoryLv1Name, _wrcategorylv1.ID.ToString());
                        _tnCategory.ChildNodes.Add(_tnCategorylv1);
                    }

                    trvCategory.Nodes.Add(_tnCategory);
                    trvCategory.CollapseAll();
                }
            }

            foreach (WRAREA _wrarea in _busArea._GetList(p => p.IDLang == lang))
            {
                string _AreaName = _wrarea.AreaName + " (" + _busRegion._GetList(p => p.IDArea == _wrarea.ID).ToList().Count + ")";
                TreeNode _tn = new TreeNode(_AreaName, _wrarea.ID.ToString());

                foreach (WRREGION _wrregion in _busRegion._GetList(p => p.IDArea == _wrarea.ID))
                {
                    string _RegionName = _wrregion.RegionName + " (" + _busCity._GetList(p => p.IDRegion == _wrregion.ID).ToList().Count + ")";
                    TreeNode _tnRegion = new TreeNode(_RegionName, _wrregion.ID.ToString());
                    _tn.ChildNodes.Add(_tnRegion);

                    foreach (WRCITY _wrcity in _busCity._GetList(p => p.IDRegion == _wrregion.ID))
                    {
                        TreeNode _tnCity = new TreeNode(_wrcity.CityName, _wrcity.ID.ToString());
                        _tnRegion.ChildNodes.Add(_tnCity);
                    }
                }


                trvLocation.Nodes.Add(_tn);
            }

            trvLocation.CollapseAll();

        }
    }

    protected void trvCategory_SelectedNodeChanged(object sender, EventArgs e)
    {
        if (trvCategory.SelectedNode.ChildNodes.Count > 0)
        {
            return;
        }
        Session["CateID"] = trvCategory.SelectedNode.Value;
        //Response.Redirect("~/" + Session["CityName"].ToString() + "/" + trvCategory.SelectedNode.Value + "/" + trvCategory.SelectedNode.Text);
        Response.Redirect("../Page/home.html");
        //Response.Redirect(Request.RawUrl);
    }

    protected void trvLocation_SelectedNodeChanged(object sender, EventArgs e)
    {
        if (trvLocation.SelectedNode.ChildNodes.Count > 0)
        {
            return;
        }
        string s = "";
        if (trvLocation.SelectedNode.Depth == 2)
        {
            Session["City"] = trvLocation.SelectedNode.Value;

        }
        else if (trvLocation.SelectedNode.Depth == 1)
        {
            Session["Region"] = trvLocation.SelectedNode.Value;
        }
        else
        {
            Session["Area"] = trvLocation.SelectedNode.Value;
        }
        //
        Response.Redirect("../Page/home.html");
        //Response.Redirect(Request.RawUrl);
    }

}